package com.regis.entity.vo;

import com.regis.comment.CodeMsg;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 结果展示类
 * @date 2022/7/28 4:38
 */
@Data
public class ResultVo<T> implements Serializable {

    private Integer code;
    private String msg = "";
    private T data;
    private Map map = new HashMap();

    public ResultVo() {
    }

    public ResultVo(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultVo(String msg) {
        this.msg = msg;
    }

    public ResultVo(T data) {
        this.data = data;
    }

    private ResultVo(CodeMsg codeMsg){
        if(!Objects.isNull(codeMsg)){
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<T>(CodeMsg.SUCCESS.getCode(),CodeMsg.SUCCESS.getMsg(), data);
    }

    public static <T> ResultVo<T> error(CodeMsg codeMsg){
        return new ResultVo<T>(codeMsg.getCode(),codeMsg.getMsg(),null);
    }

    public static <T> ResultVo<T> error(String msg){
        return new ResultVo<>(CodeMsg.DEFAULT.getCode(),msg,null);
    }

    public ResultVo<T> add(String key, Object value){
        this.map.put(key,value);
        return this;
    }
}
